<?php

$root = '../../..';
include $root.'/apps/common/lib/class.cache.php';
// SPLIT REQUEST
list($browser, $theme, $app, $widgets, $version, $debug) = explode('/', $_SERVER['QUERY_STRING'], 6);
header('content-type: text/css');
if (($css = cache::get($browser . '_' . $theme . '_' . $app . '_' . $widgets . '_' . $version . '.css')) && !$debug) {
    die($css);
} else {
    $info = <<<COMMENTS
/*
BROWSER : $browser
THEME   : $theme
APP     : $app
WIDGETS : $widgets
VERSION : $version
DEBUG   : $debug
*/\n
COMMENTS;
// THEME CSS FILES
    foreach (@glob($root . '/themes/' . $theme . '/css/*.css') as $i)
        $styles .= file_get_contents($i);
// APPLICATION TEMPLATE CSS
    $styles .= @ file_get_contents($root . '/apps/' . $app . '/template/css/' . $app . '.css');
// PAGE ITEMS CSS
    foreach (explode(',', $widgets, 20) as $app)
        foreach (@glob($root . '/apps/' . $app . '/template/css/_item.css') as $i)
            $styles .= file_get_contents($i);
// BROWSER HACK
    $styles .= @ file_get_contents($root . '/themes/' . $theme . '/browsers/' . $browser . '.css');

    if ($debug)
        die($info . $styles);
    else
        $styles = preg_replace(array(
                    '!//[^\n\r]+!',
                    '/[\r\n\t\s]+/s',
                    '#/\*.*?\*/#',
                    '/[\s]*([\{\};:])[\s]*/',
                    '/^\s+/',
                    '/}/'
                        ),
                        array(
                            '',
                            ' ',
                            '',
                            '\1',
                            '',
                            "}\n",
                        ),
                        $styles);

    cache::set($browser . '_' . $theme . '_' . $app . '_' . $widgets . '_' . $debug . '.css', $styles);
    die($styles);
}
?>